package com.example.component;


import com.example.appcontact.R;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.text.TextPaint;
import android.text.TextUtils;
import android.util.AttributeSet;
import android.view.View;
import android.view.View.MeasureSpec;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;

public class Item003Content extends View{
	
	public static final int CREATOR_FLG = 1;
	
	private  int TEXT_OFFSET_TOP;	
	private  int TEXT_OFFSET_BOTTOM;
	private  int TEXT_OFFSET_LEFT;
	
	private final int LINE_MARGIN1;
	private       int TEXT_SIZE_PHONE;
	private       int TEXT_SIZE_NAME;
	private       int TEXT_SIZE_EMAIL;
	private       int TEXT_SIZE_DAY_BIRTH;
	
	private String m_name         = "";
	private String m_phone_number = "";
	private String m_email        = "";
	private String m_daybirth     ="";
	
	private int m_color_name      = 0;
	private int m_color_phone     = 0;
	private int m_color_email     = 0;
	private int m_color_day_birth = 0;
	
	private int mHeight = 0;
	
	public void setName(String name) {
		this.m_name = name;
	}
	public void setColorName (int color){
		this.m_color_name = color;
	}
	// set phone number
	public void setPhoneNumber(String phone_number) {
		this.m_phone_number = phone_number;
	}
	public void setColorPhoneNumber(int color_phone){
		this.m_color_phone = color_phone;
	}
	 //set email 
	public void setEmail(String email){
		this.m_email = email;
	}
    public void setColorEmail(int color_email){
		this.m_color_email = color_email;
	}
   
	// set birthday
   public void setDayBirth(String daybirth){
		this.m_daybirth = daybirth;
	}
    public void setColorDayBirth(int color_day){
		this.m_color_day_birth = color_day;
	}
	public Item003Content(Context context, AttributeSet attrs) {
		super(context, attrs);
		this.m_color_name      = getResources().getColor(R.color.DimGray);
		this.m_color_phone     = getResources().getColor(R.color.Black);
		this.m_color_email     = getResources().getColor(R.color.DodgerBlue);
		this.m_color_day_birth = getResources().getColor(R.color.DimGray);

		TEXT_SIZE_NAME = (int) getResources().getDimension(R.dimen.font_size_name);
		TEXT_SIZE_PHONE = (int) getResources().getDimension(R.dimen.font_size_phone);
		TEXT_SIZE_EMAIL = (int) getResources().getDimension(R.dimen.font_size_email);
		TEXT_SIZE_DAY_BIRTH = (int) getResources().getDimension(R.dimen.font_size_day_birth);
		
		// view layout configuration
		LINE_MARGIN1    = (int) getResources().getDimension(R.dimen.popularListItemView_line_margin1);
		
		TEXT_OFFSET_TOP = (int) getResources().getDimension(R.dimen.popularListItemView_offset_top) + TEXT_SIZE_PHONE;
		TEXT_OFFSET_BOTTOM = (int) getResources().getDimension(R.dimen.popularListItemView_offset_bottom);
		TEXT_OFFSET_LEFT   = (int) getResources().getDimension(R.dimen.popularListItemView_offset_left);
	}
	@Override
	protected void onDraw(Canvas canvas) {
		super.onDraw(canvas);
		drawName(canvas);
		drawPhoneNumber(canvas);
		drawEmail(canvas);
		drawDayBirth(canvas);
		if(getHeight() < mHeight + TEXT_OFFSET_BOTTOM) {
			setLayoutParams(new RelativeLayout.LayoutParams(0, mHeight));
		}
	}
    private void drawName(Canvas canvas) {
     if(m_name != null && !m_name.equalsIgnoreCase("")){
    	 Paint paint = new Paint();
 		int width = getWidth() - TEXT_OFFSET_LEFT;
 		mHeight =  TEXT_SIZE_NAME;	
 		paint.setAntiAlias(true);
 		paint.setTextSize(TEXT_SIZE_NAME);
 		paint.setColor(m_color_name);
 		int line1Length = paint.breakText(m_name, true, width, null);
 		canvas.drawText(m_name.substring(0, line1Length), TEXT_OFFSET_LEFT, mHeight, paint);
 		if(line1Length != m_name.length()) {
 			TextPaint textPaint = new TextPaint();
 			textPaint.setTextSize(TEXT_SIZE_NAME);
 			String line2 = TextUtils.ellipsize(m_name.substring(line1Length), 
 											   textPaint, 
 											   width, 
 											   TextUtils.TruncateAt.END).toString() ;
 			mHeight = mHeight + TEXT_SIZE_NAME + 2;
 			canvas.drawText(line2, 
 							TEXT_OFFSET_LEFT, 
 							mHeight, 
 							paint);
 		}
     }
	}
    /**
	 * this method must be invoked after 'drawName' invoked
	 */
	
	private void drawPhoneNumber(Canvas canvas) {
	 if(m_phone_number != null &&  !m_phone_number.equalsIgnoreCase("")){
		 mHeight = mHeight + LINE_MARGIN1 + TEXT_SIZE_PHONE;
			Paint paint = new Paint();
			paint.setAntiAlias(true);
			paint.setTextSize(TEXT_SIZE_PHONE);
			paint.setColor(m_color_phone);
			canvas.drawText(m_phone_number, 0, mHeight, paint);	
	  }
	}
	private void drawEmail(Canvas canvas) {
		if(m_email != null &&  !m_email.equalsIgnoreCase("")){
			mHeight = mHeight + LINE_MARGIN1 + TEXT_SIZE_EMAIL;
			Paint paint = new Paint();
			paint.setAntiAlias(true);
			paint.setTextSize(TEXT_SIZE_EMAIL);
			paint.setColor(m_color_email);
			canvas.drawText(m_email, 0, mHeight, paint);	
		}
	}
	private void drawDayBirth(Canvas canvas) {
		if(m_daybirth != null &&  !m_daybirth.equalsIgnoreCase("")){
			mHeight = mHeight + LINE_MARGIN1 + TEXT_SIZE_DAY_BIRTH;
			Paint paint = new Paint();
			paint.setAntiAlias(true);
			paint.setTextSize(TEXT_SIZE_DAY_BIRTH);
			paint.setColor(m_color_day_birth);
			canvas.drawText(m_daybirth, 0, mHeight, paint);	
		}
	}
	
	private int measureHeight(int viewWidth) {
		int height;
			height = TEXT_OFFSET_TOP;
		Paint paint = new Paint();
		int width  = viewWidth - TEXT_OFFSET_LEFT;
		
		paint.setAntiAlias(true);
		paint.setTextSize(TEXT_SIZE_NAME);
		int line1Length = paint.breakText(m_name, true, width, null);
		if(line1Length != m_name.length()) {
			height = height + TEXT_SIZE_NAME + 2 ;
		}
		height = height + LINE_MARGIN1 + TEXT_SIZE_PHONE;
		height = height + LINE_MARGIN1 + TEXT_SIZE_EMAIL;
		height = height + LINE_MARGIN1 + TEXT_SIZE_DAY_BIRTH;
		height = height + LINE_MARGIN1 ;
		
		return height;
	}
	@Override
	protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
	    super.onMeasure(widthMeasureSpec, heightMeasureSpec);
	    int width = MeasureSpec.getSize(widthMeasureSpec);
	    int height = MeasureSpec.getSize(heightMeasureSpec);
	    
	    if(width > 0){
	    	height = measureHeight(width) + TEXT_OFFSET_BOTTOM;
	    }
    	this.setMeasuredDimension(width, height);
	}	
}
